# -*- coding:utf-8 -*-
from selenium.webdriver.common.by import By

from PageClass.ecsPageClass.ecsIndexPage import EcsIndexPage
from PageClass.common.boeCommon import BoeCommon
from PageClass.basePage import BasePage
from Util import logger


class BoeLogin(BasePage):
    _loginKey = (By.ID, "loginKey")
    _password = (By.ID, "password")
    _loginButton = (By.ID, "login")
    _loginSuccess = (By.XPATH, "//header")

    # 有可能出现错误提示的元素位置
    _MessageBox = (By.CLASS_NAME, 'el-message__content')
    _accountInputErrorBox = (By.XPATH, '//*[@id="app"]/div/div[1]/div/form/div[1]/div/div')
    _passwordInputErrorBox = (By.XPATH, '//*[@id="app"]/div/div[1]/div/form/div[2]/div/div')

    def __init__(self, driver):
        super().__init__(driver)
        self.driver.maximize_window()
        self.driver.implicitly_wait(10)

    @property
    def messageBox(self):
        return self._MessageBox

    @property
    def accountInputErrorBox(self):
        return self._accountInputErrorBox

    @property
    def passwordInputErrorBox(self):
        return self._passwordInputErrorBox

    @property
    def loginSuccessBox(self):
        return self._loginSuccess

    def goToLoginPage(self, url):
        self.driver.get(url)
        logger.info("打开登录页面")

    def inputAccount(self, account):
        self.clear(*self._loginKey)
        self.send_text(account, *self._loginKey)
        logger.info("输入帐号")

    def inputPassword(self, password):
        self.clear(*self._password)
        self.send_text(password, *self._password)
        logger.info("输入密码")

    def clickLoginButton(self):
        self.click(*self._loginButton)
        logger.info("点击登录")

    def getErrorMessage(self):
        return self.get_elementText(*self.messageBox)

    def getUserAccountErrorMessage(self):
        return self.get_elementText(*self.accountInputErrorBox)

    def getPasswordErrorMessage(self):
        return self.get_elementText(*self.passwordInputErrorBox)

    def getLoginSuccessMessage(self):
        return self.get_elementText(*self.loginSuccessBox)


class Class_EmployeeLoanBoe(EcsIndexPage, BoeCommon):
    # 登录界面元素
    _loginKey = (By.ID, "loginKey")
    _password = (By.ID, "password")
    _loginButton = (By.ID, "login")
    _loginSuccess = (By.XPATH, "//header")

    # 有可能出现错误提示的元素位置
    _MessageBox = (By.CLASS_NAME, 'el-message__content')
    _accountInputErrorBox = (By.XPATH, '//*[@id="app"]/div/div[1]/div/form/div[1]/div/div')
    _passwordInputErrorBox = (By.XPATH, '//*[@id="app"]/div/div[1]/div/form/div[2]/div/div')

    def __init__(self, driver):
        super().__init__(driver)
        self.driver.maximize_window()
        self.driver.implicitly_wait(10)

    @property
    def messageBox(self):
        return self._MessageBox

    @property
    def accountInputErrorBox(self):
        return self._accountInputErrorBox

    @property
    def passwordInputErrorBox(self):
        return self._passwordInputErrorBox

    @property
    def loginSuccessBox(self):
        return self._loginSuccess

    def goToLoginPage(self, url):
        self.driver.get(url)
        logger.info("打开登录页面")

    def inputAccount(self, account):
        self.clear(*self._loginKey)
        self.send_text(account, *self._loginKey)
        logger.info("输入帐号")

    def inputPassword(self, password):
        self.clear(*self._password)
        self.send_text(password, *self._password)
        logger.info("输入密码")

    def clickLoginButton(self):
        self.click(*self._loginButton)
        logger.info("点击登录")

    def getErrorMessage(self):
        return self.get_elementText(*self.messageBox)

    def getLoginSuccessMessage(self):
        return self.get_elementText(*self.loginSuccessBox)

    # 主表区-备注
    _boeAbstract = (By.ID, "boeHeader.0.boeAbstract")

    def inputboeAbstract(self, boeAbstract):
        self.clear(*self._boeAbstract)
        self.send_text(boeAbstract, *self._boeAbstract)
        logger.info("输入主表区-备注字段")

    # 发票区-请选择费用分类
    _FeeName = (By.XPATH, '//span[contains(text(),"请选择费用分类")]')

    def FeeNameClick(self):
        self.click(*self._FeeName)
        logger.info("点击'请选择费用分类'")

    # 借款明细区-业务类型
    _TypeId = (By.ID, 'loan.0.operationSubTypeId')

    def SelectType(self, text):
        self.lovSelectTable(text, *self._TypeId)
        logger.info("借款明细区选择的业务类型为：{}".format(text))

    # 借款明细区-总金额
    _AmountId = (By.ID, 'loan.0.expenseAmount')

    def inputExpenseAmount(self, text):
        self.click(*self._AmountId)
        self.send_text(text, *self._AmountId)
        logger.info("借款明细区填写的的总金额为：{}".format(text))

    # 借款明细区-项目

    def inputProject(self, text=None):
        # 点击项目字段
        _projectId = (By.ID, 'loan.0.projectId')
        if text is None:
            self.click(*_projectId)
            element_1 = (By.XPATH, '//div[@class="zte-table zte-table--mini" and @id="loan.0.projectId.table"]/div/div/div/div/div/table/tbody/tr/td/div/div[text()="1"]')
            self.click(*element_1)
        else:
            self.lovSelectTable(text, *_projectId)
        logger.info("费用归属区选择项目完成")

    BoeSuccessMsg = (By.XPATH, '//*[@id="app"]/div/div/div[4]/div/div/div[2]/div/p')

    def getBoeSuccessMessage(self):
        # 提单成功结果页面提示语
        return self.get_elementText(*self.BoeSuccessMsg)
